<template>
  <div >
    <span v-if="value >0 " style="color: #d60a22 ; font-weight: bold"> {{value}}亿 </span>
    <span v-else-if="value < 0 " style="color:#037b66 ; font-weight: bold"> {{value}}亿 </span>
    <span v-else-if="value == 0 " style="color:#ddd ;font-weight: bold"> {{value}} </span>
  </div>
</template>

<script>
export default {
  name: "BigNumberColumn",
  props:{
    value: {
      type: Number ,
      default: 0
    }
  },
  methods:{
    formatWithSeparatorAndUnit(value) {
      const formatted = this.formatChineseNumberDynamic(value);
      const numberPart = parseFloat(formatted.match(/[-\d.]+/)[0]);
      const suffix = formatted.match(/[^\d.-]+/)[0];

      // 添加千位分隔符
      return numberPart.toLocaleString('en-US') + suffix;
    },
    formatChineseNumberDynamic(value) {
      const units = [
        { threshold: 1e8, suffix: '亿', divisor: 1e8 },
        { threshold: 1e7, suffix: '千万', divisor: 1e7 },
        { threshold: 1e6, suffix: '百万', divisor: 1e6 },
        { threshold: 1e4, suffix: '万', divisor: 1e4 },
      ];

      if (value === 0) return '0';
      const isNegative = value < 0;
      value = Math.abs(value);

      for (const unit of units) {
        if (value >= unit.threshold) {
          const divided = value / unit.divisor;
          // 动态小数位数：整数显示无小数，小数部分最多两位
          const decimals = divided % 1 === 0 ? 0 : Math.min(2, 2 - Math.floor(Math.log10(divided)));
          const formatted = divided.toFixed(decimals).replace(/\.?0+$/, '');
          return (isNegative ? '-' : '') + formatted + unit.suffix;
        }
      }

      return (isNegative ? '-' : '') + value.toString();
    },
    formatLargeChineseNumber(numStr) {
      const num = BigInt(numStr.replace('.', ''));
      const units = [
        { divisor: 1e8, suffix: '亿' },  // 注意：BigInt 需手动处理小数
        { divisor: 1e7, suffix: '千万' },
        { divisor: 1e6, suffix: '百万' },
        { divisor: 1e4, suffix: '万' },
      ];

      for (const unit of units) {
        const divisor = BigInt(unit.divisor);
        if (num >= divisor) {
          const divided = Number(num / divisor) + (Number(num % divisor) / unit.divisor);
          return divided.toFixed(2).replace(/\.?0+$/, '') + unit.suffix;
        }
      }

      return numStr;
    }
  }
}
</script>

<style scoped>

</style>
